$(function(){
	/**调用初始化方法*/
	/*getAllRole();*/
	queryRole("");
});
function getAllRole(){
	$.post("/UserGetAllRoleWorker/",null,function(str){
		str = decodeURI(str);
		var data = $.parseJSON(str);
		for(var i=0;i<data.length;i++){
			$("#selectId").append("<option value='"+data[i].RoleId+"'>"+data[i].RoleName+"</option>");
		}
	});
}
/*function queryForm(){
	var userName = $("#queryName").val();
	var roleId = $("#querySelect").val();
	queryRole(userName,roleId);
}*/
function queryRole(contain){
	$(this).hide();
	$('#usertable').bootstrapTable('destroy');
	$('#usertable').bootstrapTable({
		method: 'get',
		url: '/GetAllMac/?contain='+contain,
		cache: false,
		striped: true,
		pagination:true,
		pageSize:20,
		pageList: [5, 10, 20,50],
		pageNumber:1,
		sidePagination:'server',
		height:750,
		paginationPreText: "上一页",
		paginationNextText: "下一页",     
		paginationHAlign: "left",
		paginationDetailHAlign: "right",
		columns: [
			{
				field: 'id',
				title: '序号',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},{
				field: 'quotString',
				title: '机器码',
				align: 'center',
				formatter: turnAround,
				valign: 'middle'
			},{
				field: 'room',
				title: '管理',
				align: 'center',
				formatter: operateFormatter,
				events: operateEvents
			}]
	});
}
/**
 * table图标区
 */
function operateFormatter(value, row, index) {
	if($("#update").html()=="0"||$("#delete").html()=="0"){
		if($("#update").html()=="0"&&$("#delete").html()=="0"){
			return [].join('');
		}else if($("#update").html()=="0"){
			return [
				'<button class="delete" id="delete">删除</button>'
				].join('');
		}else{
			return [
				'<button class="updateUser" id="updateUser">修改</button>&nbsp'
				].join('');
		}
	}
	return [
		'<button class="updateUser" id="updateUser">修改</button>&nbsp',
		'<button class="delete" id="delete">删除</button>'
		].join('');
}


/**
 * 冻结管理员
 * @returns
 */
$(".clockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 解冻管理员
 * @returns
 */
$(".dclockUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDClockUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
/**
 * 删除管理员
 * @returns
 */
$(".deleteUser").click(function(){
	var userids= getUserTableCheckBoxIds();
	$.post("/operationManegerDeleteUser/",{"userids":userids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getUserTableCheckBoxIds(){
	var a=$('#usertable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].uid;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].uid);
		}
		return ids;
	}
}
/**
 * 删除角色
 * @returns
 */
$(".deleteRole").click(function(){
	var roleids=getRoleTableCheckBoxIds();
	alert(roleids);
	$.post("/deleteRole/",{"roleids":roleids},function(data){
		var data1 = $.parseJSON(data);
		alert(data1.message);
    	if(data1.state == 1){
    		window.location.reload();
    	}
	});
});
function getRoleTableCheckBoxIds(){
	var a=$('#Roletable').bootstrapTable('getSelections');
	var ids="";
	if(a.length<1){
		alert("请至少选择一行操作");
	}else{
		var len=a.length;
		ids=a[0].roleId;
		for(var i=1;i<len;i++){
			ids+=(","+a[i].roleId);
		}
		return ids;
	}
}
/**
 * 图标绑定点击事件
 */
window.operateEvents = {
		'click .updateUser': function (e, value, row, index) {
			$("#titleId").html("修改限制机器码");
			$(".com_title").val(decodeURI(row.quotString,"utf-8"));
			$("#ids").val(row.id);
			$("#show_div_user").show();
		},
		'click .delete': function (e, value, row, index) {
			$.get("/deleteMac/?id="+row.id,null,function(data){
				if(data=="0"){
					alert("删除失败");
				}else{
					queryRole("");
				}
			});
		}
};

function turnAround(value, row, index){
	return decodeURI(value,"utf-8"); 
}
